package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.TResources;
import com.ruoyi.business.mapper.TResourcesMapper;
import com.ruoyi.business.service.ITResourcesService;
import com.ruoyi.business.vo.TResourcesVo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学习资源Service业务层处理
 *
 * @author Tellsea
 * @date 2023-02-17
 */
@Service
public class TResourcesServiceImpl extends ServiceImpl<TResourcesMapper, TResources> implements ITResourcesService {

    @Override
    public TableDataInfo<TResourcesVo> queryList(TResourcesVo entity) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 如果是学生，只能查询其他人上传并审核通过的+自己的所有
        if (loginUser.getUser().getRoles().get(0).getRoleId() == 3) {
            entity.setResState(1);
            Page<TResourcesVo> tResourcesVoPage = this.baseMapper.queryList(PageUtils.buildPage(), entity);
            entity.setCreateBy(loginUser.getUser().getUserName());
            entity.setResState(0);
            Page<TResourcesVo> temp1 = this.baseMapper.queryList(PageUtils.buildPage(), entity);
            entity.setResState(2);
            Page<TResourcesVo> temp2 = this.baseMapper.queryList(PageUtils.buildPage(), entity);
            List<TResourcesVo> records = tResourcesVoPage.getRecords();
            records.addAll(temp1.getRecords());
            records.addAll(temp2.getRecords());
            tResourcesVoPage.setRecords(records);
            return PageUtils.buildDataInfo(tResourcesVoPage);
        } else {
            return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
        }
    }

    @Override
    public List<TResourcesVo> queryAll(TResourcesVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public TResourcesVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }
}
